//
//  LoginViewController.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/2/24.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "LoginViewController.h"
#import "loginView.h"
#import "RegistrationView.h"
#import "AccountModel.h"
#import "AccountManager.h"
#import "AppContext.h"
#import "TPKeyboardAvoidingScrollView.h"
@interface LoginViewController (){
    UIImageView *arrowImageView;
    loginView *logView;  //登录页面
    RegistrationView *registrationView;  //注册页面
    UIButton *logBtn;
    UIButton *registBtn;
    BOOL loginViewShow;
}
@property (weak, nonatomic) IBOutlet UIView *bgVIew;
@property(strong,nonatomic)TPKeyboardAvoidingScrollView *bgScrollView;

@end

@implementation LoginViewController

+ (void)presentLoginViewController:(UIViewController *)controller withCompleteBlock:(void (^) (void))block {
    LoginViewController *vc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]];
    [vc presentLoginView];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [controller presentViewController:nav animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bgScrollView=[[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.bgScrollView.contentSize=CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view addSubview:self.bgScrollView];
    UIButton *closeBt=[UIButton buttonWithType:UIButtonTypeCustom];
    closeBt.frame=CGRectMake(265, 11, 48, 48);
    closeBt.right=SCREEN_WIDTH-10;
    [closeBt setImage:[UIImage imageNamed:@"login_close"] forState:UIControlStateNormal];
    [closeBt addTarget:self action:@selector(dismissAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgScrollView addSubview:closeBt];
    
    UIImageView *iconImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 59,48, 48)];
    iconImageView.image=[UIImage imageNamed:@"login_icon"];
    iconImageView.center=CGPointMake(SCREEN_WIDTH/2, iconImageView.center.y);
    [self.bgScrollView addSubview:iconImageView];
    [self setUI];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    if (loginViewShow) {
        [self btnAction:logBtn];
    }
}

-(void)setUI{
    float distance=(SCREEN_WIDTH-120)/3;
    UIButton *regBt=[UIButton buttonWithType:UIButtonTypeCustom];
    regBt.tag=1;
    [regBt addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    regBt.frame=CGRectMake(distance, 162, 60,30);
    [regBt setTitle:@"注册" forState:UIControlStateNormal];
    regBt.titleLabel.font=[UIFont systemFontOfSize:20];
    [regBt setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
    registBtn = regBt;
    
    UIButton *loginBt=[UIButton buttonWithType:UIButtonTypeCustom];
    [loginBt addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    loginBt.tag=2;
    loginBt.frame=CGRectMake(distance*2+60, 162, 60,30);
    [loginBt setTitle:@"登录" forState:UIControlStateNormal];
    loginBt.titleLabel.font=[UIFont systemFontOfSize:20];
    [loginBt setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
    logBtn = loginBt;
    [self.bgScrollView addSubview:regBt];
    [self.bgScrollView addSubview:loginBt];
    UIView *lineView=[[UIView alloc] initWithFrame:CGRectMake(15, 198,SCREEN_WIDTH-30, 1)];
    lineView.backgroundColor=[UIColor whiteColor];
    [self.bgScrollView addSubview:lineView];
    arrowImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 13, 6)];
    arrowImageView.image=[UIImage imageNamed:@"login_icon_zhishi"];
    arrowImageView.center=CGPointMake(regBt.center.x, 0);
    arrowImageView.bottom=lineView.bottom;
    [self.bgScrollView addSubview:arrowImageView];
    logView=[[[NSBundle mainBundle] loadNibNamed:@"loginView"  owner:self options:nil] lastObject];
    logView.width=SCREEN_WIDTH;
    logView.top=lineView.bottom;
    [self.bgScrollView addSubview: logView];
    logView.hidden=YES;
    __weak typeof(self) weakSelf = self;
    logView.LoginBlock = ^(NSString *account, NSString *password) {
        if (![account isMobileNumber]) {
            [[LKDHud shareHud] showtip:@"请输入正确的手机号"];
            return;
        }
        if (password.length <= 0) {
            [[LKDHud shareHud] showtip:@"请输入密码"];
            return;
        }
         [[LKDHud shareHud] showLoadingView];
        [ApiClient loginWithAccount:account password:password didFinishLoaded:^(AccountModel *model, NSString *path) {
             [[LKDHud shareHud] hideLoadingView];
            if ([model.status isEqual:@(1)]) {
                [weakSelf onLoginWithAccount:model];
                [weakSelf dismissAction:nil];
            }
        } didFailLoaded:^(NSError *error, NSString *path) {
             [[LKDHud shareHud] hideLoadingView];
        }];
    };
    registrationView=[[[NSBundle mainBundle] loadNibNamed:@"registrationView"  owner:self options:nil] lastObject];
    registrationView.width=SCREEN_WIDTH;
    registrationView.top=lineView.bottom;
    [self.bgScrollView addSubview: registrationView];
    
}

- (void)presentLoginView {
    loginViewShow = YES;
}

-(void)btnAction:(UIButton*)bt{
    [UIView animateWithDuration:0.2 animations:^{
        arrowImageView.center=CGPointMake(bt.center.x, arrowImageView.center.y);
    }];
    switch (bt.tag) {
        case 1:
        {
            logView.hidden=YES;
            registrationView.hidden=NO;
            if (registrationView.bottom>SCREEN_HEIGHT) {
             self.bgScrollView.contentSize=CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+100);
            }
        }
            break;
        case 2:
        {
            logView.hidden=NO;
            registrationView.hidden=YES;
            self.bgScrollView.contentSize=CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
            
        }
        default:
            break;
    }
}

#pragma mark private
- (void)onLoginWithAccount:(AccountModel *)account {
    [APP_CONTEXT.accountManager loginWithSimpleAccount:account];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)dismissAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
@end
